<template>
	<om-addpage style="padding-top: 35px;" :menuName="menuName" @formSubmit="formSubmit" :busCode="busCode">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		getUrl,
		confirmModal,
		errorModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		getEditorTitle,
		changeConfigAttr,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';

	export default {
		data() {
			return buildFormData({
				customerUserCacheKey:'bro-jy-borrowedSample-customerUser-cache',
				customerShowCacheKey:'bro-jy-borrowedSample-customerShow-cache'
			})
		},
		computed: {
			...mapStateuseLoginUserStore(useLoginUserStore, ['userName', 'userId'])
		},
		provide() {
			return {
				formPage: this
			}
		},
		methods: {
			getWorkParam(){
				let _that = this
				return {
					salePropertyIds:_that.$refs.autoform.formdata.salesAttributeCategoryId || ''
				}
			},
			onSelectPost(item){
				let _that = this
				_that.$refs.autoform.formdata.instId = item.instId
				_that.$refs.autoform.formdata.instName = item.instName
			},
			getPosComboboxByUser(id){
				let _that = this
				_that.om.request(
					"orgInstPosUserOfficial/getPosComboboxByUser",
					{
						userId:id
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor("title1","postId","data",configSelectData(res,'id','posName'))
					if(!_that.edit){
						_that.$refs.autoform.initSelectVal("title1","postId")
					}else{
						_that.$refs.autoform.initSelectIndex("title1","postId")
					}
				})
			},
			initLoad(options) {
				let _that = this;
				var fileId = options.fileId
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					{
						url:'tsysdict/getCombobox',
						param:{
							code:'39821'
						}
					},
					{
						url:'currencyDict/getLocalCurrency'
					}
				]
				if(fileId){
					ajaxArr.push(
						{
							url:'commonForm/getInfo',
							param:{
								fileId:fileId,
								formCode:options.code
							}
						}
					)
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					changeEditor("topinfo","zposId","data",configSelectData(res[0],'id','posName'),mainData)
					if(!_that.edit){
						changeEditor("title1","postId","data",configSelectData(res[0],'id','posName'),mainData)
					}
					changeEditor("title1","salesAttributeCategoryId","data",configSelectData(res[1],'id','dicname'),mainData)
					changeEditor("title1",'customerUserId',{
						link: '/pages/public/pageSearch/pageSearch',
						linkParam: {
							other: JSON.stringify({
								firstUserId: _that.userId
							}),
							namefield: 'username',
							notefield:"posInstName",
							//extrafield:"",
							searchfield: 'q',
							initload: true,
							url: 'temployee/getStaffCombogrid',
							cachename: _that.customerUserCacheKey,
							placeholder: '请输入姓名或岗位'
						}
					},null,mainData)
					changeEditor("title1",'customerShowId',{
						link: '/pages/public/pageSearch/pageSearch',
						linkParam: {
							other: JSON.stringify({
								userId: _that.userId
							}),
							namefield: 'customerCode',
							notefield:"customerId",
							extrafield:"customerName",
							searchfield: 'q',
							initload: true,
							url: 'customerShow/getCustomersByUserId',
							cachename: _that.customerShowCacheKey
						}
					},null,mainData)
					changeEditor("title1",'currencyId',{
						defaultName:res[2].value.codeNameUnit,
						defaultValue:res[2].value.id
					},null,mainData)
					if(_that.subEditorObj["productList"]){
						let attr = {
							getWorkParam:'getWorkParam'
						}
						let configData = _that.subConfigData['productList']
						if(hasEditor("productList","newProductId",configData)){
							let title = getEditorTitle("productList","newProductId",configData)
							attr.titleTemplate = title+':<%= newProductCode %>'
						}
						changeEditor("productList","dataStr",attr,mainData)
					}
					if (fileId) {
						let fieldArr = getFormFields(mainData)
						fieldArr.push('id')
						for (let index in res[3].value) {
							if (fieldArr.includes(index)) {
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata, index, res[3].value[index])
								_that.$set(_that.formdata, index, res[3].value[index])
							}
						}
						if(_that.subEditorObj["productList"]){
							let productList = _that._.cloneDeep(res[3].value.productList)
							_that.subEditorObj['productList'].workList = productList
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata, 'dataStr', JSON.stringify(
								productList))
							_that.$set(_that.formdata, 'dataStr', JSON.stringify(productList))
							delete res[3].value.productList
						}
					}
					_that.$nextTick(function(){
						if(!_that.edit){
							_that.$refs.autoform.formdata.customerUserId = _that.userId
							_that.$refs.autoform.formdata.customerUserName = _that.userName
							_that.$refs.autoform.initSelectVal("title1","postId")
						}
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.initSelectVal("topinfo","zposId")
						}else{
							_that.$refs.autoform.initSelectIndex("topinfo","zposId")
						}
					})
				}, options)
			},
			formSubmit() {
				let _that = this
				if(!_that.edit){
					_that.om.formSave("borrowedSample/doAdd",_that.formdata,_that)
				}else{
					_that.om.formSave("borrowedSample/doUpdate",_that.formdata,_that)
				}
			}
		},
		onUnload(){
		//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
		//#endif
		},
		onLoad(options) {
			this.initValue.userinfoId = this.userId
			this.initLoad(options)
		},
		onShow() {
			let _that = this;
			if (_that.$refs.autoform) {
				let customerMoney = 0
				for (var fieldId in _that.subEditorObj) {
					var editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							for(let item of res.data){
								if(item.productMoneyAll){
									customerMoney += Number(item.productMoneyAll)
								}
							}
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
				_that.$refs.autoform.formdata.customerMoney = customerMoney
				uni.getStorage({
					key:_that.customerUserCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.customerUserId = d.id
							formdata.customerUserName = d.username
							_that.getPosComboboxByUser(d.id)
							uni.removeStorage({
								key:_that.customerUserCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.customerShowCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.customerShowId = d.id
							formdata.customerCodeX = d.customerCode
							formdata.customerId = d.customerId
							formdata.customerNameX = d.customerName
							uni.removeStorage({
								key:_that.customerShowCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>
       